const { STATUS_CODE } = require('../../../constants');
const dbService = require('../../../service/dbService');
const userExist = async (ctx, next) => {
  let { name } = ctx.request.body;
  const user = await dbService.query({ name }, 'users');
  if (user[0] && user[0].id) {
    ctx.app.emit('error', new Error(STATUS_CODE.USER_ALREADY_EXIST), ctx);
    return;
  }
  await next();
};

module.exports = userExist;
